package iwb.readt.utils;

import iwb.utils.RedisUtils;
import iwb.utils.StringUtils;
import org.redisson.api.RBucket;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;
import java.util.function.Function;

@Component
public class DuplicateUpdateFilter {

    public static final String SPLIT = "_";
    @Autowired
    private RedisUtils redisUtils;

    private static final String KEY_START = "RUNLAST";

    private static final String MEMO_START = "memostart";


    private static final String TICKET_START = "ticketstart";

    /**
     * setKeyToRedis
     * @param function Function<T,String>
     * @param t T
     * @param <T> <T>
     * @return String
     */
    public <T>  String setKeyToRedis(Function<T,String> function,T t){
        String id = function.apply(t);
        RBucket<String> bucket = redisUtils.getRedissonClient().getBucket(KEY_START+ SPLIT +id);
        long time = System.currentTimeMillis();
        String timeStr= String.valueOf(time);
        bucket.set(timeStr);
        bucket.expire(60, TimeUnit.SECONDS);
        return timeStr;
    }


    /**
     * setKeyToRedis
     * @param id
     * @return
     */
    public  String setKeyToRedis(String id){
        RBucket<String> bucket = redisUtils.getRedissonClient().getBucket(KEY_START+ SPLIT +id);
        long time = System.currentTimeMillis();
        String timeStr= String.valueOf(time);
        bucket.set(timeStr);
        bucket.expire(60, TimeUnit.SECONDS);
        return timeStr;
    }

    /**
     * isRunCurrentSession
     * @param id String
     * @param timeStr String
     * @return boolean
     */
    public boolean isRunCurrentSession(String id,String timeStr){
        RBucket<String> bucket = redisUtils.getRedissonClient().getBucket(KEY_START+ SPLIT +id);
        String time = bucket.get();
        if(time==null || time.equals(timeStr)){
            bucket.delete();
            return true;
        } else {
            return false;
        }

    }


    public boolean isProcessMemo(String memoId){
        RBucket<String> bucket = redisUtils.getRedissonClient().getBucket(MEMO_START+ SPLIT +memoId);
        String memo = bucket.get();
        if(StringUtils.isEmpty(memo)){
            bucket.set("1");
            bucket.expire(60, TimeUnit.SECONDS);
            return false;
        } else {
            return true;
        }
    }

    public void deleteProcessMemo(String memoId){
        RBucket<String> bucket = redisUtils.getRedissonClient().getBucket(MEMO_START+ SPLIT +memoId);
        bucket.delete();
    }


    public boolean isProcessTicket(String ticketId){
        RBucket<String> bucket = redisUtils.getRedissonClient().getBucket(TICKET_START+ SPLIT +ticketId);
        String memo = bucket.get();
        if(StringUtils.isEmpty(memo)){
            bucket.set("1");
            bucket.expire(60, TimeUnit.SECONDS);
            return false;
        } else {
            return true;
        }
    }

    public void deleteProcessTicket(String ticketId){
        RBucket<String> bucket = redisUtils.getRedissonClient().getBucket(TICKET_START+ SPLIT +ticketId);
        bucket.delete();
    }




}
